
//
//  FZVistorView.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/27.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//
import UIKit
import SnapKit
class FZVistorView: UIView {

    //添加旋转图片
   private lazy var rotateImg = UIImageView(image: UIImage(named : "visitordiscover_feed_image_smallicon"))
    //添加遮罩图片
   private lazy var maskImg = UIImageView(image: UIImage(named : "visitordiscover_feed_mask_smallicon"))
    
   private lazy var imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
   var closure : (()->())?
   //文字label
   private lazy var messageLab : UILabel = {
       let lab = UILabel()
       lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
       lab.textColor = UIColor.darkGray
       lab.font = UIFont.systemFont(ofSize: 13)
       lab.numberOfLines = 0
       return lab
    }()
    
    private lazy var registLab : UIButton = {
        
       let registLab = UIButton()
       registLab.setBackgroundImage(UIImage(named : "common_button_white_disable"), for:.normal)
       registLab.setTitle("注册", for: .normal)
       registLab.setTitleColor(UIColor.darkGray, for: .normal)
       registLab.setTitleColor(UIColor.orange, for: .highlighted)
       registLab.titleLabel?.font = UIFont.systemFont(ofSize: 14)
       registLab.adjustsImageWhenHighlighted = false
       
       registLab.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
       return registLab
    }()
    
    private lazy var loginLab : UIButton = {
        
        let loginLab = UIButton()
        loginLab.setBackgroundImage(UIImage(named : "common_button_white_disable"), for:.normal)
        loginLab.setTitle("登陆", for: .normal)
        loginLab.setTitleColor(UIColor.darkGray, for: .normal)
        loginLab.setTitleColor(UIColor.orange, for: .highlighted)
        loginLab.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        loginLab.adjustsImageWhenHighlighted = false //取消高亮效果
        loginLab.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        return loginLab
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0/255, alpha: 1)
        addSubview(rotateImg)
        addSubview(maskImg)
        addSubview(messageLab)
        addSubview(registLab)
        addSubview(loginLab)
        addSubview(imgView)
        //snapkit布局
        rotateImg.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
        maskImg.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            make.size.equalTo(rotateImg)
        }
        messageLab.snp.makeConstraints { (make) in
            make.width.equalTo(224)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImg.snp.bottom).offset(30)
        }
        
        registLab.snp.makeConstraints { (make) in
            make.left.equalTo(messageLab)
            make.width.equalTo(60)
            make.height.equalTo(30)
            make.top.equalTo(messageLab.snp.bottom).offset(10)
        }
        
        loginLab.snp.makeConstraints { (make) in
            make.right.equalTo(messageLab)
            make.width.equalTo(60)
            make.height.equalTo(30)
            make.top.equalTo(messageLab.snp.bottom).offset(10)
        }
        
        imgView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
    }
   @objc private func loginClick() {

        closure?()
   }
    
    func changeVistorView(imageName : String? , message : String?) {
        
        if let imgStr = imageName , let mes = message {
            imgView.image = UIImage(named: imgStr)
            messageLab.text = mes
            rotateImg.image = nil
            
        }else{
            startAnimation()
        }
        
    }
    func startAnimation() {
        
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        animator.fromValue = 0
        animator.toValue = M_PI * 2
        animator.repeatCount = MAXFLOAT
        animator.duration = 20
        animator.isRemovedOnCompletion = false
        rotateImg.layer.add(animator, forKey: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
